package com.mio.kududemo;

import java.util.LinkedList;

import org.apache.kudu.ColumnSchema;
import org.apache.kudu.Schema;
import org.apache.kudu.Type;
import org.apache.kudu.client.KuduClient;
import org.apache.kudu.client.KuduException;

public class Kududemo {

	public static void main(String[] args) throws KuduException {
		// master地址
				String masterAddr = "linux170";
				// 创建kudu的数据库连接
				KuduClient client = new KuduClient.KuduClientBuilder(masterAddr)
						.defaultSocketReadTimeoutMs(60000).build();

		try {
			
		
		System.out.println("start....");
		client.deleteTable("student");
		// ==============创建表=============
		/*
		 * 定义Schema
		 */
		LinkedList<ColumnSchema> columns = new LinkedList<>();
		ColumnSchema id = new ColumnSchema.ColumnSchemaBuilder("id", Type.INT32).key(true).build();
		ColumnSchema name = new ColumnSchema.ColumnSchemaBuilder("name", Type.STRING).key(false).build();
		columns.add(id);
		columns.add(name);
		Schema schema = new Schema(columns);
		System.out.println("creat....");
		KuduUtil.createTable(client, schema, "student", "id");
		KuduUtil.createTable(client, schema, "student2", "id");

		// 删除表
		System.out.println("delete....");
		client.deleteTable("student2");
		
		// 插入数据
		System.out.println("insert....");
		KuduUtil.insert(client, "student", 1, "mio1");
		KuduUtil.insert(client, "student", 2, "mio2");
		KuduUtil.insert(client, "student", 3, "mio3");

		// 查询数据
		 System.out.println("===student table====");
//		KuduUtil.search(client, "student");
		// 更改表数据
		 System.out.println("===mio3 is changed to lucy===");
         KuduUtil.update(client, "student", 3, "lucy");

		// 删除数据
         KuduUtil.delete(client, "student", 2);
         System.out.println("===mio2 is deleted===");
//         KuduUtil.search(client, "student");
		
		} catch (Exception e) {
			e.printStackTrace();
		}finally {
			client.close();
		}
	}
}
